package com.wash.car.web.controller.applets;

import cn.hutool.core.collection.ListUtil;
import com.pmo.boot.common.api.BaseResult;
import com.pmo.boot.common.api.DefaultResult;
import com.wash.car.biz.OrderServer;
import com.wash.car.dao.model.*;
import com.wash.car.web.config.context.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = {"订单管理"})
@RestController
@RequestMapping(path = "/applets/order")
public class AppletsOrderController {

    @Autowired
    private OrderServer orderServer;


    @ApiOperation(value = "开始排队")
    @PostMapping(path = "queue")
    public BaseResult queue(@RequestBody OrderAddModel model) {
        model.setUserId(UserContext.get().getUserId());
        orderServer.add(model);
        return BaseResult.success();
    }


    @ApiOperation(value = "取消排队")
    @PostMapping(path = "cancelQueue")
    public BaseResult cancelQueue(@RequestBody OrderDeleteModel model) {
        orderServer.del(model.setUserId(UserContext.get().getUserId()));
        return BaseResult.success();
    }


    @ApiOperation(value = "查询门店排队数量")
    @GetMapping(path = "getStoreQueueCount/{storeId}")
    public DefaultResult<StoreQueueModel> getStoreQueueCount(@PathVariable(value = "storeId") @ApiParam(name = "storeId", value = "门店ID", required = true) Long storeId) {
        return DefaultResult.success(orderServer.findStoreQueueCount(storeId));
    }


    @ApiOperation(value = "获取用户车辆信息")
    @GetMapping(path = "getUserVehicles")
    public DefaultResult<List<String>> getUserVehicles() {
        Long userId = UserContext.get().getUserId();
        List<String> list = orderServer.findVehicles(userId);
        return DefaultResult.success(list);
    }


    @ApiOperation(value = "获取用户订单信息")
    @GetMapping(path = "getUserOrders")
    public DefaultResult<List<OrderDetailsModel>> getUserOrders(OrderWhereModel model) {
        model.setUserId(UserContext.get().getUserId());
        List<OrderDetailsModel> list = orderServer.findOrderDetails(model);
        return DefaultResult.success(list);
    }


    @ApiOperation(value = "获取用户排队订单信息")
    @GetMapping(path = "getUserQueueOrders")
    public DefaultResult<List<OrderDetailsModel>> getUserQueueOrders() {
        Long userId = UserContext.get().getUserId();
        OrderWhereModel model = new OrderWhereModel().setUserId(userId)
                .setStates(ListUtil.toList(new Integer[]{0, 2, 3}));
        List<OrderDetailsModel> list = orderServer.findOrderQueueDetails(model);
        return DefaultResult.success(list);
    }


}
